/*
 * BusinessFacadeDelegate.java
 *
 * Created on 07 March 2006, 17:02
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package web.util;

import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.user.UserEntity;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import se392.ateam2006.authentication.GroupEntity;
import se392.ateam2006.authentication.GroupMgmtRemote;
import se392.ateam2006.meetingmgmt.MeetingMgmtRemote;
import se392.ateam2006.meetingmgmt.meeting.Duration;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.ResourceMgmtRemote;
import se392.ateam2006.resourcemgmt.participant.ParticipationEntity;
import se392.ateam2006.resourcemgmt.room.RoomEntity;
import se392.ateam2006.user.UserMgmtRemote;

/**
 *
 * @author Phil
 */
public class BusinessFacadeDelegate implements Serializable {

    //private static final String MEETING_MGMT_REMOTE_REF = "java:comp/env/ejb/MeetingMgmtBean";
    MeetingMgmtRemote meetingMgmt;
    UserMgmtRemote userMgmt;
    ResourceMgmtRemote resourceMgmt;
    GroupMgmtRemote groupMgmt;
    UserEntity user;

    /** Creates a new instance of BusinessFacadeDelegate */
    public BusinessFacadeDelegate(UserEntity user) throws Exception {
        System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        try {
            InitialContext jndicontext = new InitialContext();
            meetingMgmt = (MeetingMgmtRemote) jndicontext.lookup("se392.ateam2006.meetingmgmt.MeetingMgmtRemote");

            resourceMgmt = (ResourceMgmtRemote) jndicontext.lookup("se392.ateam2006.resourcemgmt.ResourceMgmtRemote");

            userMgmt = (UserMgmtRemote) jndicontext.lookup("se392.ateam2006.user.UserMgmtRemote");

            groupMgmt = (GroupMgmtRemote) jndicontext.lookup("se392.ateam2006.authentication.GroupMgmtRemote");
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getDurationInMinutes(Duration duration) throws Exception {
        return meetingMgmt.getDurationInMins(duration);
    }

    public UserEntity getLoggedInUser() {
        return user;
    }

    public void setLoggedInUser(UserEntity user) {
        this.user = user;
    }

    public void createUser(UserEntity user) throws Exception {
        userMgmt.createUser(user);
    }

    public UserEntity getUser(String email) throws Exception {
        return userMgmt.getUserByID(email);
    }
    
     public void addGroup(GroupEntity group) {
        groupMgmt.addGroup(group.getGroupid());
    }
    
    public void addUserToGroup(UserEntity user, GroupEntity group) {
        groupMgmt.addUserToGroup(group.getGroupid(), user.getEmail());
    }
    
    public GroupEntity getGroup(String groupId) {
        return groupMgmt.getGroupById(groupId);
    }
    
    public Collection<GroupEntity> getAllGroups() {
        return groupMgmt.getAllGroups();
    }

    public String checkUserExists(String email, String muid) {
        UserEntity userEnt = userMgmt.getUserByID(email);
        MeetingEntity meetingEnt = meetingMgmt.findMeetingById(muid);
        if (resourceMgmt.getParticipant(userEnt, meetingEnt) == null) {
            return "E200";
        }
        return "Participant Exists";
    }

    //Meeting stuff
    public MeetingEntity createMeeting(UserEntity initiator, String id, String title, String description,
            GregorianCalendar startDateRange, GregorianCalendar endDateRange,
            Duration duration, int requestedRoomSize, String equipment, RoomEntity room) throws Exception {
            meetingMgmt.createMeeting(initiator, id, title, description, startDateRange, endDateRange, duration, requestedRoomSize, equipment, room);
            return meetingMgmt.findMeetingById(id);
    }

    public MeetingEntity getMeeting(String meetingId) throws FinderException, RemoteException {
        return meetingMgmt.findMeetingById(meetingId);
    }

    public String checkMeetingExists(String meetingId) {
        if (meetingMgmt.findMeetingById(meetingId) == null) {
            return "false";
        }
        return "Meeting Exists";
    }

    public void addParticipants(String meetingId, ArrayList<ParticipationEntity> participants) throws Exception, RemoteException {
        for (ParticipationEntity participant : participants) {
            MeetingEntity me = meetingMgmt.findMeetingById(meetingId);
            resourceMgmt.createParticipation(me, participant.getUserEntity(), participant.isActive());
        }
    }
    
    public Collection<RoomEntity> getAllRoomsAllowed(UserEntity user) {
        return resourceMgmt.getAllRoomsAllowed(user);
    }
    
    public void addRoom(RoomEntity room) {
        resourceMgmt.createRoom(room);
    }
    
    public Collection<MeetingEntity> getUserMeetings(UserEntity user) {
        return meetingMgmt.findMeetingsByInitiator(user);
    }
}
